<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" size="small">
      <el-form-item label="问题大类" prop="code">
        <el-select filterable v-model="queryParamsCode" placeholder="请选择问题大类" clearable size="small">
          <el-option v-for="dict in questionList" :key="parseInt(dict.id)" :label="dict.name" :value="dict.code" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="问题大类" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入问题大类" clearable />
            </el-form-item> -->
      <el-form-item label="问题名称" prop="name">
        <el-input v-if="queryParamsCode != 'implosion'" v-model="queryParams.name" placeholder="请输入问题名称" clearable />
        <el-select @change="$forceUpdate()" v-if="queryParamsCode == 'implosion'" v-model="queryParams.name"
          placeholder="请选择问题名称" clearable size="small">
          <el-option v-for="dict in getDictDatas(DICT_TYPE.pc_implosion_type)" :key="parseInt(dict.value)" :label="dict.label" :value="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>

      </el-form-item>
    </el-form>
    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['pekingfish:questionconfig:add']" type="primary" icon="el-icon-plus" size="mini"
          @click="openDialog">新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getTableData"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="操作" align="center">
        <template v-slot="scope">
          <el-button v-hasPermi="['pekingfish:questionconfig:update']" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="问题类型" prop="categoryName" align="center" />
      <el-table-column label="问题名称" prop="name" align="center" />
      <el-table-column label="是否展示" prop="isShow" align="center">
        <template v-slot="scope">
          <el-tag type="primary" v-if="scope.row.isShow==1">是</el-tag>
          <el-tag type="info" v-if="scope.row.isShow==0">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="平台分润比例" prop="platformRatio" align="center" />
      <el-table-column label="积分获取比例" prop="pointsRatio" align="center" />
      <el-table-column label="更新时间" prop="updateTime" align="center">
        <template v-slot="scope">
          {{ parseTime(scope.row.updateTime) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template v-slot="scope">
          <el-switch :disabled="scope.row.code == 'implosion' ? true : false" @change="changeSwitch(scope.row)"
            v-model="scope.row.status" :active-value="0" :inactive-value="1" />
        </template>
      </el-table-column>

    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getTableData" />

    <el-dialog :title="dialogText" :visible.sync="dialogVisible" width="600px">
      <el-form :model="dialogParams" ref="dialogForm" :rules="rules">
        <el-form-item label="问题大类" prop="code">
          <el-select style="width: 100%;" :disabled="dialogParams.id ? true : false" v-model="dialogParams.code"
            placeholder="请选择问题大类" clearable size="small">
            <el-option v-for="dict in questionList" :key="parseInt(dict.id)" :label="dict.name" :value="dict.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="问题名称" prop="name" v-if="dialogParams.code != 'implosion'">
          <el-input v-model="dialogParams.name" placeholder="请输入问题名称" clearable />
        </el-form-item>
        <el-form-item label="问题名称" prop="name" v-if="dialogParams.code == 'implosion'">
          <el-select style="width: 100%;" @change="$forceUpdate()" v-model="dialogParams.name" placeholder="请选择问题名称"
            clearable>
            <el-option v-for="dict in getDictDatas(DICT_TYPE.pc_implosion_type)" :key="parseInt(dict.value)" :label="dict.label" :value="dict.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否展示" prop="isShow">
          <el-radio-group v-model="dialogParams.isShow">
            <el-radio v-for="dict in isShow_status" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="平台分润比例(%)" prop="platformRatio">
          <!-- <el-input v-model="dialogParams.platformRatio" placeholder="请输入平台分润比例" clearable  style="width: 240px" /> -->
          <el-input-number style="width: 100%;" v-model="dialogParams.platformRatio" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="积分获取比例(%)" prop="pointsRatio">
          <!-- <el-input v-model="dialogParams.pointsRatio" placeholder="请输入积分获取比例" clearable  style="width: 240px" />% -->
          <el-input-number style="width: 100%;" v-model="dialogParams.pointsRatio" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dialogParams.status" :disabled="dialogParams.categoryId == 8002 ? true : false">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.pc_enable_disable_status)" :key="dict.value"
              :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button :loading="loading" type="primary" @click="confirmDialog('dialogForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getQuestionTableList,
  getQuestion,
  addQuestionList,
  updateQuestionList,
} from "@/api/configuration/questionType";
export default {
  name: "questionType",
  data() {
    return {
      showSearch: true,
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        code: undefined,
      },
      total: 0,
      questionList: [],
      tableData: [],
      loading: false,
      dialogText: "新增",
      dialogVisible: false,
      // 表单校验
      rules: {
        categoryId: [
          { required: true, message: "请选择问题大类", trigger: "change" },
        ],
        name: [
          { required: true, message: "问题名称不能为空", trigger: "blur" },
        ],
        platformRatio: [
          { required: true, message: "平台分润比例不能为空", trigger: "blur" },
        ],
        pointsRatio: [
          { required: true, message: "积分获取比例不能为空", trigger: "blur" },
        ],
      },
      dialogParams: {
        categoryId: 0,
        name: "",
      },
      questionTypeList: [
        { name: "有内爆", id: 1 },
        { name: "无内爆", id: 2 },
      ],
      isShow_status: [
        { label: '是', value: 1 },
        { label: '否', value: 0 }
      ],
      queryParamsCode: null
    };
  },
  created() {
    this.getQuestionList();
    this.getTableData();
  },
  methods: {
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
      };
      this.queryParamsCode = null;
      this.getTableData();
    },
    handleQuery() {
      let question = this.questionList.find((item) => item.code == this.queryParamsCode);
      this.queryParams.categoryId = question?question.id:null;
      this.getTableData();
    },
    async getTableData() {
      this.loading = true;
      const { data } = await getQuestionTableList(this.queryParams);
      this.loading = false;
      this.total = data.total;
      this.tableData = data.list;
    },
    async getQuestionList() {
      const { data } = await getQuestion();
      this.questionList = data;
    },
    /** 取消按钮 */
    cancel() {
      this.dialogVisible = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.dialogParams = {
        id: undefined,
        categoryId: undefined,
        code: undefined,
        name: undefined,
        platformRatio: 0,
        pointsRatio: 0,
        status: 0,
        isShow:1
      };
      this.resetForm("dialogForm");
    },
    openDialog() {
      this.reset();
      this.dialogText = "新增";
      this.dialogVisible = true;
    },
    confirmDialog(data) {
      this.$refs[data].validate((valid) => {
        if (valid) {
          let question = this.questionList.find((item) => item.code == this.dialogParams.code);
          this.dialogParams.categoryId = question?question.id:null;
          this.loading = true;
          if (!this.dialogParams.id) {
            addQuestionList(this.dialogParams).then((res) => {
              if (res.data) {
                this.dialogVisible = false;
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                this.getTableData();
              }
              this.loading = false
            }).catch(f => {
              this.loading = false
            })
          } else {
            updateQuestionList(this.dialogParams).then((res) => {
              if (res.data) {
                this.dialogVisible = false;
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                this.getTableData();
              }
              this.loading = false
            }).catch(f => {
              this.loading = false
            })
          }
        }
      });
    },
    handleUpdate(data) {
      this.reset();
      this.dialogText = "编辑";
      this.dialogVisible = true;
      this.dialogParams = JSON.parse(JSON.stringify(data));;
    },
    changeSwitch(data) {
      updateQuestionList(data).then((res) => {
        if (res.data) {
          this.dialogVisible = false;
          this.$message({
            message: "操作成功",
            type: "success",
          });
          this.getTableData();
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
